﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Madason.GenUtils;
using System.Xml;
using Madason.MetaModel;

namespace Madason.Templates.Schema
{
    public class Program
    {



        /// <summary>
        /// main function
        /// - parse command argumends
        /// - invoke special generator-functions
        /// </summary>
        /// <param name="args"></param>
        [STAThread]
        public static int Main(string[] args)
        {
            try
            {
                GeneratorWorkflow workflow = new GeneratorWorkflow(args);
                workflow.CheckCommandLine();
                workflow.LoadModel();
                workflow.Validate();

                foreach (Class cls in workflow.Model.FindClassesByStereotype("Schema"))
                {
                    Console.Out.WriteLine("------------- " + cls.Name + ".xsd ---------------");
                    SchemaGenerator gen = new SchemaGenerator(cls);
                    using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
                    {
                        writer.Formatting = Formatting.Indented;
                        gen.Generate(writer);
                    }
                    Console.Out.WriteLine();
                    Console.Out.WriteLine("------------------------------------------------");
                }
                return 0;
            }
            catch (Madason.GenUtils.ValidationException e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
            catch (Madason.GenUtils.MissingArgumentException e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }

        }
    }

}
